﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; 
using System.Text;
using Common;

public partial class website_admin_knowlegebase_newseditor : PageBase
{  
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin();

        /// 保存设置
        if (RequestBase.GetFormString("___EVENTTARGET") == "save")
        {
            Save();
        }

        if (!IsPostBack)
        {
            if (RequestBase.GetQueryString("articleid") != "")
                Initialize();
        }
    }
 
    #region 数据初始化
    void Initialize()
    {
        Model.KbArticleBase _obj = BLL.KbArticleBase.GetObjById(RequestBase.GetQueryString("articleid"));
        ArticleObj = _obj;
    }

    Model.KbArticleBase ArticleObj
    {
        get
        {
            Model.KbArticleBase _article = new Model.KbArticleBase();

            if (!string.IsNullOrEmpty(RequestBase.GetQueryString("articleid")))
                _article.ArticleId = RequestBase.GetQueryString("articleid");

            _article.Title = txtTitle.Value.Trim();

            if (txtArticleNumber.Value.Trim() == "AUTO")
            {
                _article.ArticleNumber = Decimal.Parse(BLL.KbArticleBase.GetArticleNumber(RequestBase.GetQueryString("c_number")));
            }
            else
                _article.ArticleNumber = Decimal.Parse(string.IsNullOrEmpty(txtArticleNumber.Value.Trim()) && Utils.IsNumber(txtArticleNumber.Value) ? "0" : txtArticleNumber.Value.Trim());

            _article.CategoryId = BLL.KbCategoryBase.GetCategoryIdByNumber(RequestBase.GetQueryString("c_number"));
            _article.NewsURL = txtNewsURL.Value.Trim();
            _article.Content = txtContent.Text;
            _article.Source = txtAuthor.Value.Trim();
            _article.SourceType = (cbxSourceType.Checked) ? 1 : 0;

            _article.ModifiedOn = DateTime.Now;
            _article.CreatedOn = DateTime.Now;
            _article.ModifiedBy = this.SystemUserId;
            _article.CreatedBy = this.SystemUserId; 

            return _article;
        }
        set
        {
            if (value != null)
            {
                txtTitle.Value = value.Title.Trim();
                txtArticleNumber.Value = value.ArticleNumber.ToString();
                txtNewsURL.Value = value.NewsURL.Trim();
                txtAuthor.Value = value.Source.Trim();
                cbxSourceType.Checked = (value.SourceType == 1);
                txtContent.Text = value.Content; 
            }
        }
    }
    #endregion

    #region 保存数据
    void Save()
    {
        string res = "";

        if (RequestBase.GetQueryString("otype").Trim() == "add")
        {
            res = BLL.KbArticleBase.Add(ArticleObj);
        }

        if (RequestBase.GetQueryString("otype").Trim() == "modify")
        {
            res = BLL.KbArticleBase.Update(ArticleObj);
        }

        switch (res.Split('|')[0])
        {
            case "succeeded":
                JScript.AlertAndReturnValueToParent("Information saved successfully!", "window.opener.LoadingData(); window.close();");
                break;
            default:
                JScript.Alert(res);
                break;
        }
    }
    #endregion
}
